package com.kezesong.leasing.controller.admin;

import com.kezesong.leasing.common.ResponseResult;
import com.kezesong.leasing.controller.BaseController;
import com.kezesong.leasing.controller.request.admin.AddGoodsLogisticsReqBody;
import com.kezesong.leasing.controller.request.admin.ModifyGoodsColorReqBody;
import com.kezesong.leasing.entity.GoodsLogisticsEntity;
import com.kezesong.leasing.service.GoodsLogisticsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin")
public class GoodsLogisticsManagerController extends BaseController {

    @Autowired
    private GoodsLogisticsService goodsLogisticsService;

    @PostMapping("/getAllGoodsLogisticList.do")
    @ApiOperation(value = "getAllGoodsLogisticList", notes = "管理员获取所有商品物流")
    public ResponseResult<List<GoodsLogisticsEntity>> getAllGoodsRAMInfoList(){
        List<GoodsLogisticsEntity> goodsLogistics = goodsLogisticsService.getGoodsLogistics();

        if (null == goodsLogistics || goodsLogistics.size() == 0){
            return buildNoDataResponse();
        }else {
            ResponseResult<List<GoodsLogisticsEntity>> listResponseResult = buildSucessResponse(goodsLogistics);
            return listResponseResult;
        }
    }

    @GetMapping("/getGoodsLogisticsByGoodsId/{goodsId}")
    @ApiOperation(value = "getGoodsLogisticsByGoodsId", notes = "根据商品id获取某个商品的物流（发货地址）")
    public ResponseResult<GoodsLogisticsEntity> getGoodsLogisticsByGoodsId(@PathVariable("goodsId") int goodsId){
        GoodsLogisticsEntity goodsLogisticsEntity =goodsLogisticsService.getGoodsLogistics(goodsId);
        if (null == goodsLogisticsEntity){
            return buildNoDataResponse();
        }else {
            ResponseResult<GoodsLogisticsEntity> responseResult = buildSucessResponse(goodsLogisticsEntity);
            return responseResult;
        }
    }

    @PostMapping("/addGoodsLogistics.do")
    @ApiOperation(value = "addGoodsLogistics", notes = "管理员添加商品的物流发货地")
    public ResponseResult<String> addGoodsLogistics(@RequestBody AddGoodsLogisticsReqBody reqBody){
        GoodsLogisticsEntity goodsLogisticsEntity = new GoodsLogisticsEntity();
        BeanUtils.copyProperties(reqBody, goodsLogisticsEntity);
        int rows = goodsLogisticsService.addGoodsLogistics(goodsLogisticsEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }

    @GetMapping("/deleteGoodsLogisticsByGoodsId/{goodsId}")
    @ApiOperation(value = "deleteGoodsLogisticsByGoodsId", notes = "管理员删除商品物流")
    public ResponseResult<String> deleteGoodsLogisticsByGoodsId(@PathVariable("goodsId") int goodsId){
        int rows  =goodsLogisticsService.deleteGoodsLogistics(goodsId);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }


    @PostMapping("/modifyGoodsLogistics.do")
    @ApiOperation(value = "modifyGoodsLogistics", notes = "管理员修改商品物流")
    public ResponseResult<String> modifyGoodsLogistics(@RequestBody ModifyGoodsColorReqBody reqBody){
        GoodsLogisticsEntity goodsLogisticsEntity = new GoodsLogisticsEntity();
        BeanUtils.copyProperties(reqBody,goodsLogisticsEntity);
        int rows = goodsLogisticsService.modifyGoodsLogistics(goodsLogisticsEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }
}
